package com.jack006.article.service.impl;

        import com.github.pagehelper.PageHelper;
        import com.jack006.api.service.BaseService;
        import com.jack006.article.mapper.CommentsMapper;
        import com.jack006.article.mapper.CommentsMapperCustom;
        import com.jack006.article.service.ArticlePortalService;
        import com.jack006.article.service.CommentPortalService;
        import com.jack006.exception.GraceException;
        import com.jack006.grace.result.ResponseStatusEnum;
        import com.jack006.pojo.Comments;
        import com.jack006.pojo.vo.ArticleDetailVO;
        import com.jack006.pojo.vo.CommentsVO;
        import com.jack006.utils.PagedGridResult;
        import org.n3r.idworker.Sid;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        import org.springframework.transaction.annotation.Transactional;

        import java.util.Date;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

/**
 * @Classname CommentPortalServiceImpl
 * @Description 文章留言服务实现类
 * @Date 2021-07-16 08:25
 * @Created by jack
 */
@Service
public class CommentPortalServiceImpl extends BaseService implements CommentPortalService {


    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private CommentsMapperCustom commentsMapperCustom;
    @Autowired
    private ArticlePortalService articlePortalService;
    @Autowired
    private Sid sid;

    @Transactional
    @Override
    public void createComments(String articleId, String fatherId, String comment,
                               String userId, String nickName,String userFace) {

        String commentId = sid.nextShort();

        ArticleDetailVO articleDetailVO = articlePortalService.queryDetail(articleId);

        Comments comments = new Comments();

        comments.setWriterId(articleDetailVO.getPublishUserId());
        comments.setId(commentId);
        comments.setArticleId(articleId);
        comments.setArticleTitle(articleDetailVO.getTitle());
        comments.setArticleCover(articleDetailVO.getCover());

        comments.setFatherId(fatherId);
        comments.setCommentUserNickname(nickName);
        comments.setCommentUserId(userId);
        comments.setCommentUserFace(userFace);

        comments.setContent(comment);
        comments.setCreateTime(new Date());

        int result = commentsMapper.insert(comments);
        if (result != 1) {
            GraceException.display(ResponseStatusEnum.SYSTEM_OPERATION_ERROR);
        }

        redisOperator.increment(REDIS_ARTICLE_COMMENTS_COUNTS+":"+articleId,1);
    }

    @Override
    public PagedGridResult queryArticleComments(String articleId, Integer page, Integer pageSize) {

        Map<String,Object> map = new HashMap<>();
        map.put("articleId",articleId);

        PageHelper.startPage(page,pageSize);
        List<CommentsVO> commentsVOS = commentsMapperCustom.queryArticleCommentList(map);
        return setterPagedGrid(commentsVOS,page);
    }

    @Override
    public void deleteComment(String writerId, String commentId) {
        Comments comments = new Comments();
        comments.setId(commentId);
        comments.setWriterId(writerId);

        commentsMapper.delete(comments);
    }

    @Override
    public PagedGridResult queryWriterCommentsMng(String writerId, Integer page, Integer pageSize) {
        Comments comment = new Comments();
        comment.setWriterId(writerId);

        PageHelper.startPage(page, pageSize);
        List<Comments> list = commentsMapper.select(comment);
        return setterPagedGrid(list, page);
    }
}
